home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.5 / shutil.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-05-11  |  6.5 KB  |  273 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. """Utility functions for copying files and directory trees.
  5.  
  6. XXX The functions here don't copy the resource fork or other metadata on Mac.
  7.  
  8. """
  9. import os
  10. import sys
  11. import stat
  12. from os.path import abspath
  13. __all__ = [
  14.     'copyfileobj',
  15.     'copyfile',
  16.     'copymode',
  17.     'copystat',
  18.     'copy',
  19.     'copy2',
  20.     'copytree',
  21.     'move',
  22.     'rmtree',
  23.     'Error']
  24.  
  25. class Error(EnvironmentError):
  26.     pass
  27.  
  28.  
  29. def copyfileobj(fsrc, fdst, length = 16384):
  30.     '''copy data from file-like object fsrc to file-like object fdst'''
  31.     while None:
  32.         buf = fsrc.read(length)
  33.         if not buf:
  34.             break
  35.         
  36.         continue
  37.         return None
  38.  
  39.  
  40. def _samefile(src, dst):
  41.     if hasattr(os.path, 'samefile'):
  42.         
  43.         try:
  44.             return os.path.samefile(src, dst)
  45.         except OSError:
  46.             return False
  47.         except:
  48.             None<EXCEPTION MATCH>OSError
  49.         
  50.  
  51.     None<EXCEPTION MATCH>OSError
  52.     return os.path.normcase(os.path.abspath(src)) == os.path.normcase(os.path.abspath(dst))
  53.  
  54.  
  55. def copyfile(src, dst):
  56.     '''Copy data from src to dst'''
  57.     if _samefile(src, dst):
  58.         raise Error, '`%s` and `%s` are the same file' % (src, dst)
  59.     
  60.     fsrc = None
  61.     fdst = None
  62.     
  63.     try:
  64.         fsrc = open(src, 'rb')
  65.         fdst = open(dst, 'wb')
  66.         copyfileobj(fsrc, fdst)
  67.     finally:
  68.         if fdst:
  69.             fdst.close()
  70.         
  71.         if fsrc:
  72.             fsrc.close()
  73.         
  74.  
  75.  
  76.  
  77. def copymode(src, dst):
  78.     '''Copy mode bits from src to dst'''
  79.     if hasattr(os, 'chmod'):
  80.         st = os.stat(src)
  81.         mode = stat.S_IMODE(st.st_mode)
  82.         os.chmod(dst, mode)
  83.     
  84.  
  85.  
  86. def copystat(src, dst):
  87.     '''Copy all stat info (mode bits, atime and mtime) from src to dst'''
  88.     st = os.stat(src)
  89.     mode = stat.S_IMODE(st.st_mode)
  90.     if hasattr(os, 'utime'):
  91.         os.utime(dst, (st.st_atime, st.st_mtime))
  92.     
  93.     if hasattr(os, 'chmod'):
  94.         os.chmod(dst, mode)
  95.     
  96.  
  97.  
  98. def copy(src, dst):
  99.     '''Copy data and mode bits ("cp src dst").
  100.  
  101.     The destination may be a directory.
  102.  
  103.     '''
  104.     if os.path.isdir(dst):
  105.         dst = os.path.join(dst, os.path.basename(src))
  106.     
  107.     copyfile(src, dst)
  108.     copymode(src, dst)
  109.  
  110.  
  111. def copy2(src, dst):
  112.     '''Copy data and all stat info ("cp -p src dst").
  113.  
  114.     The destination may be a directory.
  115.  
  116.     '''
  117.     if os.path.isdir(dst):
  118.         dst = os.path.join(dst, os.path.basename(src))
  119.     
  120.     copyfile(src, dst)
  121.     copystat(src, dst)
  122.  
  123.  
  124. def copytree(src, dst, symlinks = False):
  125.     '''Recursively copy a directory tree using copy2().
  126.  
  127.     The destination directory must not already exist.
  128.     If exception(s) occur, an Error is raised with a list of reasons.
  129.  
  130.     If the optional symlinks flag is true, symbolic links in the
  131.     source tree result in symbolic links in the destination tree; if
  132.     it is false, the contents of the files pointed to by symbolic
  133.     links are copied.
  134.  
  135.     XXX Consider this example code rather than the ultimate tool.
  136.  
  137.     '''
  138.     names = os.listdir(src)
  139.     os.makedirs(dst)
  140.     errors = []
  141.     for name in names:
  142.         srcname = os.path.join(src, name)
  143.         dstname = os.path.join(dst, name)
  144.         
  145.         try:
  146.             if symlinks and os.path.islink(srcname):
  147.                 linkto = os.readlink(srcname)
  148.                 os.symlink(linkto, dstname)
  149.             elif os.path.isdir(srcname):
  150.                 copytree(srcname, dstname, symlinks)
  151.             else:
  152.                 copy2(srcname, dstname)
  153.         continue
  154.         except (IOError, os.error):
  155.             why = None
  156.             errors.append((srcname, dstname, str(why)))
  157.             continue
  158.             except Error:
  159.                 err = None
  160.                 errors.extend(err.args[0])
  161.                 continue
  162.             
  163.         try:
  164.             copystat(src, dst)
  165.         except WindowsError:
  166.             None<EXCEPTION MATCH>(IOError, os.error)
  167.             None<EXCEPTION MATCH>(IOError, os.error)
  168.         except OSError:
  169.             why = None
  170.             errors.extend((src, dst, str(why)))
  171.         except:
  172.             None<EXCEPTION MATCH>(IOError, os.error)
  173.  
  174.         if errors:
  175.             raise Error, errors
  176.         
  177.  
  178.  
  179. def rmtree(path, ignore_errors = False, onerror = None):
  180.     '''Recursively delete a directory tree.
  181.  
  182.     If ignore_errors is set, errors are ignored; otherwise, if onerror
  183.     is set, it is called to handle the error with arguments (func,
  184.     path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
  185.     path is the argument to that function that caused it to fail; and
  186.     exc_info is a tuple returned by sys.exc_info().  If ignore_errors
  187.     is false and onerror is None, an exception is raised.
  188.  
  189.     '''
  190.     if ignore_errors:
  191.         
  192.         def onerror(*args):
  193.             pass
  194.  
  195.     elif onerror is None:
  196.         
  197.         def onerror(*args):
  198.             raise 
  199.  
  200.     
  201.     names = []
  202.     
  203.     try:
  204.         names = os.listdir(path)
  205.     except os.error:
  206.         err = None
  207.         onerror(os.listdir, path, sys.exc_info())
  208.  
  209.     for name in names:
  210.         fullname = os.path.join(path, name)
  211.         
  212.         try:
  213.             mode = os.lstat(fullname).st_mode
  214.         except os.error:
  215.             mode = 0
  216.  
  217.         if stat.S_ISDIR(mode):
  218.             rmtree(fullname, ignore_errors, onerror)
  219.             continue
  220.         
  221.         try:
  222.             os.remove(fullname)
  223.         continue
  224.         except os.error:
  225.             err = None
  226.             onerror(os.remove, fullname, sys.exc_info())
  227.             continue
  228.         
  229.  
  230.     
  231.     
  232.     try:
  233.         os.rmdir(path)
  234.     except os.error:
  235.         None<EXCEPTION MATCH>os.error
  236.         None<EXCEPTION MATCH>os.error
  237.         onerror(os.rmdir, path, sys.exc_info())
  238.     except:
  239.         None<EXCEPTION MATCH>os.error
  240.  
  241.  
  242.  
  243. def move(src, dst):
  244.     '''Recursively move a file or directory to another location.
  245.  
  246.     If the destination is on our current filesystem, then simply use
  247.     rename.  Otherwise, copy src to the dst and then remove src.
  248.     A lot more could be done here...  A look at a mv.c shows a lot of
  249.     the issues this implementation glosses over.
  250.  
  251.     '''
  252.     
  253.     try:
  254.         os.rename(src, dst)
  255.     except OSError:
  256.         if os.path.isdir(src):
  257.             if destinsrc(src, dst):
  258.                 raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
  259.             
  260.             copytree(src, dst, symlinks = True)
  261.             rmtree(src)
  262.         else:
  263.             copy2(src, dst)
  264.             os.unlink(src)
  265.     except:
  266.         os.path.isdir(src)
  267.  
  268.  
  269.  
  270. def destinsrc(src, dst):
  271.     return abspath(dst).startswith(abspath(src))
  272.  
  273.